{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f1aa5c50",
   "metadata": {},
   "source": [
    "# 语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "12913a0b",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'bdasr'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbdasr\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m fetch_token,asr\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'bdasr'"
     ]
    }
   ],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "377549c6",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'bdasr'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Input \u001b[1;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mbdasr\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'bdasr'"
     ]
    }
   ],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f41bc102",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96c8304c",
   "metadata": {},
   "source": [
    "# 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "95830ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备自己的key\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'\n",
    "\n",
    "# 文件路径\n",
    "AUDIO_FILE = 'speech-demo-master/rest-api-asr/python/audio/16k.m4a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5ff7546c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 录制音频\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dc4de55c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'bdasr' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [14]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m xu_token \u001b[38;5;241m=\u001b[39m \u001b[43mbdasr\u001b[49m\u001b[38;5;241m.\u001b[39mfetch_token(API_KEY,SECRET_KEY)\n\u001b[0;32m      2\u001b[0m bdasr\u001b[38;5;241m.\u001b[39masr(xu_token,AUDIO_FILE)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'bdasr' is not defined"
     ]
    }
   ],
   "source": [
    "xu_token = bdasr.fetch_token(API_KEY,SECRET_KEY)\n",
    "bdasr.asr(xu_token,AUDIO_FILE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b3c1519e",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'chat'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Input \u001b[1;32mIn [13]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mchat\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'chat'"
     ]
    }
   ],
   "source": [
    "import chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfe96c49",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat.openai_chat(\"你好\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f9e442d0",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'bdasr'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Input \u001b[1;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbdasr\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m fetch_token,asr\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'bdasr'"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ba03b66c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'fetch_token' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m xu_token \u001b[38;5;241m=\u001b[39m \u001b[43mfetch_token\u001b[49m(API_KEY,SECRET_KEY)\n\u001b[0;32m      2\u001b[0m xu_token\n",
      "\u001b[1;31mNameError\u001b[0m: name 'fetch_token' is not defined"
     ]
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65328ed0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e1a1f32",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bf08caf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca579d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = opchat.openai_chat(asr_output)\n",
    "chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69599517",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30b399ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition # 语音音频录制模块\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "from bdasr import fetch_token,asr # token 和语音识别模块\n",
    "from bdtts import tts # 语音合成模块\n",
    "\n",
    "# 1. 录制音频\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到 wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "print(\"----完成录制----\")\n",
    "    \n",
    "# 2. 语音识别\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "print(\"----完成语音识别----\",asr_output)\n",
    "\n",
    "# 3. openai- API\n",
    "chat = opchat.openai_chat(asr_output)\n",
    "print(chat)\n",
    "\n",
    "# 4. 语音合成 TTS -->产生result.wav\n",
    "tts(xu_token,chat)\n",
    "print(\"----完成合成---\")\n",
    "\n",
    "# 5. 在jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n",
    "print(\"---完成播放---\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
